package com.oocl.ajax.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.ajax.factory.ServiceFactory;
import com.oocl.ajax.log4j.Log4j;
import com.oocl.ajax.pojo.Area;
import com.oocl.ajax.service.AreaService;
import com.oocl.ajax.util.JsonUtil;

/**
 * Servlet implementation class LoadAreasServlet
 */
public class LoadAreasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AreaService areaService;

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		areaService = ServiceFactory.getAreaService();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Access-Control-Allow-Origin", "*"); //允许跨域访问
		response.setContentType("text/html;charset=utf-8");
		System.out.println("comming");
		String name = request.getParameter("key");
		System.out.println(name);
		if(name != null && name !=""){
			System.out.println("---------------------"+name);
			Log4j.getLogger().info("服务器接收到的信息为{}", name);
			List<Area> areas = areaService.queryByName(name);
			
			for(Area a:areas){
				System.out.println(a.toString());
			}
			response.getWriter().write(JsonUtil.parsetToStrings(areas));
			
		}
		
	}

}
